import {Request, Response} from "express";
import {checkParams} from "@src/util/util";
import httpStatusCodes from "@src/declarations/major/HttpStatusCodes";
import Constant from "@src/util/constant";
import articleServer from "@src/services/articleServer";
import articleSql from "@src/sql/articleSql";

const paths = {
  add: "/article",
  delete: "/article/:id",
  update: "/article/:id",
  list: "/article",
  recommend: "/article/update-recommend/:id",
  grounding: "/article/update-grounding/:id",
  detail: "/article-detail/:id",
  ranking: "/article-ranking",
};

// 添加文章
const add = async (req: Request, res: Response) => {
  const flag = checkParams(req.body, ["username", "category_id", "cover_id", "name"]);
  if (!flag) {
    return res.status(httpStatusCodes.BAD_REQUEST).json({
      data: Constant.LACK,
    });
  }
  const data = await articleServer.add(req);
  return res.status(data.code).json({
    data,
  });
};

// 删除文章
const _delete = async (req: Request, res: Response) => {
  const {id} = req.params;
  if (id === "undefined") {
    return res.status(httpStatusCodes.BAD_REQUEST).json({
      data: Constant.LACK,
    });
  } else {
    // 文章数据不存在
    if (!(await articleSql.isArticleId(id))) {
      return res.status(httpStatusCodes.BAD_REQUEST).json({
        data: Constant.ARTICLE_NOT_ERROR,
      });
    }
    await articleSql._delete(id);
    return res.status(httpStatusCodes.OK).json({
      data: Constant.DEFAULT_SUCCESS,
    });
  }
};

// 更新文章
const update = async (req: Request, res: Response) => {
  const flag = checkParams(req.body, ["username", "category_id", "cover_id", "name"]);
  if (!flag) {
    return res.status(httpStatusCodes.BAD_REQUEST).json({
      data: Constant.LACK,
    });
  }
  // 更新
  const data = await articleServer.update(req);
  return res.status(data.code).json({
    data,
  });
};

// 文章列表
const list = async (req: Request, res: Response) => {
  const page = req.query.page ? req.query.page : 1;
  const size = req.query.size ? req.query.size : 15;
  const filters = req.query.filters ? req.query.filters : {};
  const data = await articleServer.list(Number(page), Number(size), filters);
  return res.status(data.code).json({data});
};

// 是否首页推荐
const updateRecommend = async (req: Request, res: Response) => {
  const {id} = req.params;
  const {recommend} = req.body;
  if (id === "undefined" || (!recommend && recommend !== 0)) {
    return res.status(httpStatusCodes.BAD_REQUEST).json({
      data: Constant.LACK,
    });
  }

  const data = await articleServer.updateRecommend(id, recommend);
  return res.status(httpStatusCodes.OK).json({data});
};

// 更新是否上架
const updateGrounding = async (req: Request, res: Response) => {
  const {id} = req.params;
  const {grounding} = req.body;
  if (id === "undefined" || (!grounding && grounding !== 0)) {
    return res.status(httpStatusCodes.BAD_REQUEST).json({
      data: Constant.LACK,
    });
  }
  const data = await articleServer.updateGrounding(id, grounding);
  return res.status(httpStatusCodes.OK).json({data});
};

// 文章详情
const detail = async (req: Request, res: Response) => {
  const {id} = req.params;
  const data: any = await articleServer.detail(id);
  return res.status(data.code).json({
    data,
  });
};

// 文章排名
const ranking = async (req: Request, res: Response) => {
  const data = await articleSql.ranking();
  return res.status(data.code).json({
    data,
  });
};


export default {
  paths,
  add,
  _delete,
  update,
  list,
  updateRecommend,
  updateGrounding,
  detail,
  ranking,
};